package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateTableMapping;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移表映射信息类-[表名: 迁移表映射信息(migrate_table_mapping)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateTableMappingDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateTableMappingDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateTableMapping(MigrateTableMapping migrateTableMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_table_mapping ( "
                + "table_mapping_id,subs_code,table_mapping_way,source_table_id,source_table_code,source_table_name,source_sql,target_table_id,target_table_code,target_table_name"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateTableMapping.getTableMappingId());
            ps.setObject(2, migrateTableMapping.getSubsCode());
            ps.setObject(3, migrateTableMapping.getTableMappingWay());
            ps.setObject(4, migrateTableMapping.getSourceTableId());
            ps.setObject(5, migrateTableMapping.getSourceTableCode());
            ps.setObject(6, migrateTableMapping.getSourceTableName());
            ps.setObject(7, migrateTableMapping.getSourceSql());
            ps.setObject(8, migrateTableMapping.getTargetTableId());
            ps.setObject(9, migrateTableMapping.getTargetTableCode());
            ps.setObject(10, migrateTableMapping.getTargetTableName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateTableMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateTableMapping migrateTableMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_table_mapping where 1=1"
                + " AND table_mapping_id = ? "
                );
            ps.setObject(1, migrateTableMapping.getTableMappingId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateTableMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateTableMapping migrateTableMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_table_mapping set "
                + " table_mapping_id = ? ,"
                + " subs_code = ? ,"
                + " table_mapping_way = ? ,"
                + " source_table_id = ? ,"
                + " source_table_code = ? ,"
                + " source_table_name = ? ,"
                + " source_sql = ? ,"
                + " target_table_id = ? ,"
                + " target_table_code = ? ,"
                + " target_table_name = ? "
                + " where 1=1"
                + " AND table_mapping_id = ? "
                );
            ps.setObject(1, migrateTableMapping.getTableMappingId());
            ps.setObject(2, migrateTableMapping.getSubsCode());
            ps.setObject(3, migrateTableMapping.getTableMappingWay());
            ps.setObject(4, migrateTableMapping.getSourceTableId());
            ps.setObject(5, migrateTableMapping.getSourceTableCode());
            ps.setObject(6, migrateTableMapping.getSourceTableName());
            ps.setObject(7, migrateTableMapping.getSourceSql());
            ps.setObject(8, migrateTableMapping.getTargetTableId());
            ps.setObject(9, migrateTableMapping.getTargetTableCode());
            ps.setObject(10, migrateTableMapping.getTargetTableName());
            ps.setObject(11, migrateTableMapping.getTableMappingId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateTableMapping queryByPk(MigrateTableMapping migrateTableMapping) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateTableMapping result = null;
        try {
            ps = conn.prepareStatement("select table_mapping_id,subs_code,table_mapping_way,source_table_id,source_table_code,source_table_name,source_sql,target_table_id,target_table_code,target_table_namefrom migrate_table_mapping where 1=1 "
                + " AND table_mapping_id = ? "
                );
            ps.setObject(1, migrateTableMapping.getTableMappingId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateTableMapping();
				result.setTableMappingId(rs.getString("table_mapping_id"));
				result.setSubsCode(rs.getString("subs_code"));
				result.setTableMappingWay(rs.getString("table_mapping_way"));
				result.setSourceTableId(rs.getString("source_table_id"));
				result.setSourceTableCode(rs.getString("source_table_code"));
				result.setSourceTableName(rs.getString("source_table_name"));
				result.setSourceSql(rs.getString("source_sql"));
				result.setTargetTableId(rs.getString("target_table_id"));
				result.setTargetTableCode(rs.getString("target_table_code"));
				result.setTargetTableName(rs.getString("target_table_name"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateTableMapping 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateTableMapping> queryAll(MigrateTableMapping migrateTableMapping) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateTableMapping> result = new ArrayList<>();
        try {
            String sql = "select table_mapping_id,subs_code,table_mapping_way,source_table_id,source_table_code,source_table_name,source_sql,target_table_id,target_table_code,target_table_namefrom migrate_table_mapping where 1=1 ";
                if (migrateTableMapping.getTableMappingId() != null) {
                    sql = sql + " AND table_mapping_id =  '" + migrateTableMapping.getTableMappingId() + "'";
                }
                if (migrateTableMapping.getSubsCode() != null) {
                    sql = sql + " AND subs_code =  '" + migrateTableMapping.getSubsCode() + "'";
                }
                if (migrateTableMapping.getTableMappingWay() != null) {
                    sql = sql + " AND table_mapping_way =  '" + migrateTableMapping.getTableMappingWay() + "'";
                }
                if (migrateTableMapping.getSourceTableId() != null) {
                    sql = sql + " AND source_table_id =  '" + migrateTableMapping.getSourceTableId() + "'";
                }
                if (migrateTableMapping.getSourceTableCode() != null) {
                    sql = sql + " AND source_table_code =  '" + migrateTableMapping.getSourceTableCode() + "'";
                }
                if (migrateTableMapping.getSourceTableName() != null) {
                    sql = sql + " AND source_table_name =  '" + migrateTableMapping.getSourceTableName() + "'";
                }
                if (migrateTableMapping.getSourceSql() != null) {
                    sql = sql + " AND source_sql =  '" + migrateTableMapping.getSourceSql() + "'";
                }
                if (migrateTableMapping.getTargetTableId() != null) {
                    sql = sql + " AND target_table_id =  '" + migrateTableMapping.getTargetTableId() + "'";
                }
                if (migrateTableMapping.getTargetTableCode() != null) {
                    sql = sql + " AND target_table_code =  '" + migrateTableMapping.getTargetTableCode() + "'";
                }
                if (migrateTableMapping.getTargetTableName() != null) {
                    sql = sql + " AND target_table_name =  '" + migrateTableMapping.getTargetTableName() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateTableMapping obj = new MigrateTableMapping();
				obj.setTableMappingId(rs.getString("table_mapping_id"));
				obj.setSubsCode(rs.getString("subs_code"));
				obj.setTableMappingWay(rs.getString("table_mapping_way"));
				obj.setSourceTableId(rs.getString("source_table_id"));
				obj.setSourceTableCode(rs.getString("source_table_code"));
				obj.setSourceTableName(rs.getString("source_table_name"));
				obj.setSourceSql(rs.getString("source_sql"));
				obj.setTargetTableId(rs.getString("target_table_id"));
				obj.setTargetTableCode(rs.getString("target_table_code"));
				obj.setTargetTableName(rs.getString("target_table_name"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
